// views/posts/posts/post-detail/post-detail.js
var postsData=require("../../../data/postData.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    var postId=option.id
    this.data.postId = postId
    var postData = postsData.postList[postId]
    this.setData({
      postData:postData
    })
    // 从缓存中取值看是否收藏
    var postsCollected=wx.getStorageSync("postsCollected")
    var postCollected = postsCollected[postId]
    if (postCollected){
      this.setData({
        collected:postCollected
      })
    }else{
      if (postsCollected) { } else { var postsCollected = {}}
      postsCollected[postId]=false
      wx.setStorageSync("postsCollected", postsCollected)
    }
  },
  // 收藏
  collect:function(e){
    var postsCollected = wx.getStorageSync("postsCollected")
    var collected = postsCollected[this.data.postId]
    this.setData({
      collected: !collected
    })
    postsCollected[this.data.postId]=!collected
    wx.setStorageSync("postsCollected", postsCollected)
    wx.showToast({
      title: collected?"取消成功":"收藏成功"
    })
  },
  // 分享
  share: function (e) {
    var itemlist = [
      "分享到好友",
      "分享到朋友圈",
      "分享到QQ"
    ]
    wx.showActionSheet({
      itemList: itemlist ,
      itemColor: '#405f80',
      success: function(res) {
        wx.showModal({
          title: '用户'+itemlist[res.tapIndex],
          cancelText: '取消',
          showCancel:true,
          cancelColor: '',
          confirmText: '确定',
          confirmColor: '',
          success: function(res) {},
          fail: function(res) {},
          complete: function(res) {},
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})